package com.xiaodimall.beforesys.service.impl;

import com.xiaodimall.common.Constants;
import com.xiaodimall.common.IndexConfigTypeEnum;
import com.xiaodimall.beforesys.mapper.GoodsMapper;
import com.xiaodimall.beforesys.mapper.IndexMsgMapper;
import com.xiaodimall.beforesys.pojo.Goods;
import com.xiaodimall.beforesys.pojo.IndexConfig;
import com.xiaodimall.beforesys.service.IndexMsgService;
import com.xiaodimall.utils.BeanUtil;
import com.xiaodimall.beforesys.vo.IndexConfigGoodsVO;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;


/**
 * @author wyc
 * @description
 * @ClassName IndexMsgServiceImpl
 * @date 2022/11/25 15:35
 */
@Service
public class IndexMsgServiceImpl implements IndexMsgService {


    //注入首页信息持久层
    @Resource
    IndexMsgMapper indexMsgMapper;
    //注入商品持久层
    @Resource
    GoodsMapper goodsMapper;



    /**
     * 显示首页数据
     * @author WYC
     * @param session
     * @return: mall/index
     * @date: 2022/11/25 19:49
     */
    @Override
    public String getIndexMsg(HttpServletRequest session) {
        // 根据商品类型IndexConfigTypeEnum.INDEX_GOODS_HOT=3 和显示数量Constants.INDEX_GOODS_HOT_NUMBER=4  配置热销商品
        List<IndexConfigGoodsVO> hostGoods = IndexGoodsMsg(IndexConfigTypeEnum.INDEX_GOODS_HOT.getType(), Constants.INDEX_GOODS_HOT_NUMBER);
        // 根据商品类型IndexConfigTypeEnum.INDEX_GOODS_HOT=4 和显示数量Constants.INDEX_GOODS_HOT_NUMBER=5  配置新品商品
        List<IndexConfigGoodsVO> newGoods = IndexGoodsMsg(IndexConfigTypeEnum.INDEX_GOODS_NEW.getType(), Constants.INDEX_GOODS_NEW_NUMBER);
        // 根据商品类型IndexConfigTypeEnum.INDEX_GOODS_HOT=5 和显示数量Constants.INDEX_GOODS_HOT_NUMBER=10  配置推荐商品
        List<IndexConfigGoodsVO> rmdGoods =IndexGoodsMsg(IndexConfigTypeEnum.INDEX_GOODS_RECOMMOND.getType(), Constants.INDEX_GOODS_RECOMMOND_NUMBER);
        session.setAttribute("hotGoodses", hostGoods);//热销商品
        session.setAttribute("newGoodses", newGoods);//新品
        session.setAttribute("recommendGoodses", rmdGoods);//推荐商品
        return "mall/index";

    }


    /**
     * 封装一个处理首页数据的方法
     * @author WYC
     * @param configType
     * @param number
     * @return: 返回集合
     * @date: 2022/11/25 19:47
     */
    @Override
    public List<IndexConfigGoodsVO> IndexGoodsMsg(int configType, int number) {
        //创建一个只封装五条数据的集合
        List<IndexConfigGoodsVO> indexConfigGoodsVOS = new ArrayList<>(number);
        //调用根据商品类型和显示指定数量的方法
        List<IndexConfig> indexConfigs = indexMsgMapper.getGoodsForIndex(configType, number);
        if (!CollectionUtils.isEmpty(indexConfigs)) {
            //取出所有的goodsId
            ArrayList<Long> goodsIds = new ArrayList<>();
            for (IndexConfig indexConfig : indexConfigs) {
                goodsIds.add(indexConfig.getGoodsId());
            }
            //调用获取首页数据的方法
            List<Goods> newBeeMallGoods = goodsMapper.selectByPrimaryKeys(goodsIds);
            //把查询出来的数据转换到另一个封装类
            indexConfigGoodsVOS = BeanUtil.copyList(newBeeMallGoods, IndexConfigGoodsVO.class);
            // 解决字符串过长导致文字超出的问题
            for (IndexConfigGoodsVO newBeeMallIndexConfigGoodsVO : indexConfigGoodsVOS) {
                String goodsName = newBeeMallIndexConfigGoodsVO.getGoodsName();
                String goodsIntro = newBeeMallIndexConfigGoodsVO.getGoodsIntro();
                if (goodsName.length() > 30) {
                    goodsName = goodsName.substring(0, 30) + "...";
                    newBeeMallIndexConfigGoodsVO.setGoodsName(goodsName);
                }
                if (goodsIntro.length() > 22) {
                    goodsIntro = goodsIntro.substring(0, 22) + "...";
                    newBeeMallIndexConfigGoodsVO.setGoodsIntro(goodsIntro);
                }
            }
        }
        return indexConfigGoodsVOS;
    }
}
